<template>
  <div style="margin-left:50px">
    <el-form ref="deptForm" :model="deptForm" :rules="deptRules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="deptForm.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="deptForm.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item style="margin-left:150px">
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    id: {
      type: String,
      require: true
    },
    isEdit: {
      type: Boolean,
      require: true
    }
  },
  data() {
    return {
      deptForm: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      deptRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }
        ]
      },
      employees: []
    }
  },
  computed: {
  },
  created() {
    this.loadDept()
    this.loadDetail()
  },
  methods: {
    async doAdd() {
      const params = { ...this.deptForm, pid: this.id }
      const result = await addDepartments(params)
      console.log(result, '')
      this.$emit('success')
      this.$refs.deptForm.resetFields()
    },
    async doEdit() {
      const result = await updateDepartments(this.deptForm)
      this.$emit('success')
      this.$message.success(result.message)
    },
    hSubmit() {
      // 补充兜底校验
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    hCancel() {
      this.$refs.deptForm.resetFields()
      this.$emit('cancle')
    },
    async loadDept() {
      const res = await getEmployeeSimple()
      console.log(res, '获取部门负责人')
      this.employees = res.data
    },
    async loadDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.id)
        this.deptForm = res.data
      }
    }
  }
}
</script>
<style lang='scss'  scoped>
</style>
